package com.learning.jquizenglish.utility;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.MainActivity;
import com.learning.jquizenglish.common.fragment.JQuizTaskFragment;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.ProfileModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.model.ResultModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.network.JQuizEnglishRequest;
import com.learning.jquizenglish.screen.learningfeed.CommentAdapter;
import com.learning.jquizenglish.screen.profile.ProfileScreen;
import com.nostra13.universalimageloader.core.ImageLoader;

public class DialogUtil {

	public static void showCommentDialog(final Activity act,final QuestionModel model,final OnDialogListener listener){
		final Dialog dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(act).inflate(R.layout.layout_comment, null);
		final RelativeLayout layoutWarperComment = (RelativeLayout) vContent.findViewById(R.id.layoutWraper);
		final ProgressBar prgLoading = (ProgressBar) vContent.findViewById(R.id.prgLoading);
		final EditText edtComment = (EditText) vContent.findViewById(R.id.edtComment);
		Button btnSend = (Button) vContent.findViewById(R.id.btnSend);
		Button btnCancel = (Button) vContent.findViewById(R.id.btnCancel);
		final ListView listview = (ListView) vContent.findViewById(R.id.listComment);
		Spinner spError = (Spinner) vContent.findViewById(R.id.spError);
		final ArrayList<String> ret = new ArrayList<String>();
		final ArrayList<String> arrFlashCardLevel = new ArrayList<String>();
		arrFlashCardLevel.add("None");
		arrFlashCardLevel.add("This question is out of date");
		arrFlashCardLevel.add("Typo/spelling/grammar");
		arrFlashCardLevel.add("Distributing answer");
		ret.add("");
	    ArrayAdapter<String> spAdapter = new ArrayAdapter<String>(act, R.layout.spinner_item_category, arrFlashCardLevel);
	    spAdapter.setDropDownViewResource(R.layout.spinner_item_black);
	    spError.setAdapter(spAdapter);
	    spError.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				String select = arrFlashCardLevel.get(position);
				if (!select.equals(arrFlashCardLevel.get(0))) {
					ret.set(0, select);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		JQuizRequestModel requestModel = JQuizEnglishApi.getRequestQuestionOne(model.id);
		JQuizEnglishRequest.executeGetTaskReturnList(act, null, requestModel, false, QuestionModel.class, new JQuizTaskFragment.JQuizTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if (object!=null) {
					ArrayList<QuestionModel> listModel = (ArrayList<QuestionModel>) object;
					QuestionModel model = null;
					if (listModel!=null && listModel.size()>0) {
						model = listModel.get(0);
					}
					if (listview!=null && model!=null) {
						int commentItemHeight = act.getResources().getDimensionPixelSize(R.dimen.comment_item_height);
						CommentAdapter adapter = new CommentAdapter(act, model.comments);
						listview.setAdapter(adapter);
						int listviewElementsheight = 0;
						int numberItemVisible = 0;
						for (int i = 0; i < adapter.getCount(); i++) {
					        View mView = adapter.getView(i, null, listview);
					        mView.measure(
					                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
					                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
					        listviewElementsheight += mView.getMeasuredHeight();
					        numberItemVisible = i;
					        if (i==5) break;
					    }
						layoutWarperComment.getLayoutParams().height = listviewElementsheight+commentItemHeight;
					}
				}
				prgLoading.setVisibility(View.GONE);
			}
		});
		btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				String edt = edtComment.getText().toString().trim();
				JQuizRequestModel requestModel = null;
				if (!ret.get(0).equals("")) {
					requestModel = JQuizEnglishApi.getRequestPostComment(act,model.id, ret.get(0)+"-"+edt);
				}else{
					requestModel = JQuizEnglishApi.getRequestPostComment(act,model.id,edt);
				}
				JQuizEnglishRequest.executePostTask(act,null,requestModel, false, ResultModel.class, null);
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onCancel();
				}
			}
		});
		int dialogWidth = (MainActivity.SCREEN_WIDTH*8)/9;
		dialog.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
		dialog.show();
	}
	
	public static void showHelpDialog(Activity act,QuestionModel model,final OnDialogListener listener){
		final Dialog dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(act).inflate(R.layout.layout_question_help, null);
		TextView txtHelp = (TextView) vContent.findViewById(R.id.txtHelp);
		if(model!=null){
			txtHelp.setText(model.content);
		}
		Button btnSend = (Button) vContent.findViewById(R.id.btnSend);
		Button btnCancel = (Button) vContent.findViewById(R.id.btnCancel);
		btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onPositive(null);
				}
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onCancel();
				}
			}
		});
		int dialogWidth = (MainActivity.SCREEN_WIDTH*8)/9;
		dialog.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
		dialog.show();
	}
	
	public static void showMoreDialog(Activity act,final OnDialogListener listener){
		final Dialog dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(act).inflate(R.layout.layout_more, null);
		TextView txtDontShowIt = (TextView) vContent.findViewById(R.id.txtDontShow);
		txtDontShowIt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onPositive(null);
				}
			}
		});
		int dialogWidth = (MainActivity.SCREEN_WIDTH*8)/9;
		dialog.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
		dialog.show();
	}
	
	public static void showWhyDontShowDialog(Activity act,final OnDialogListener listener){
		final Dialog dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(act).inflate(R.layout.layout_why_dont_show, null);
		TextView txtMaster = (TextView) vContent.findViewById(R.id.txtMaster);
		TextView txtIsNotUseFull = (TextView) vContent.findViewById(R.id.txtIsNotUseFull);
		txtMaster.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onPositive(null);
				}
			}
		});
		txtIsNotUseFull.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onPositive(null);
				}
			}
		});
		int dialogWidth = (MainActivity.SCREEN_WIDTH*8)/9;
		dialog.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
		dialog.show();
	}
	
	public static void showUserInfoDialog(Activity act,ProfileModel model){
		final Dialog dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(act).inflate(R.layout.dialog_user_info, null);
		ImageView imgAvatar = (ImageView) vContent.findViewById(R.id.imgAvatar);
		TextView txtUser = (TextView) vContent.findViewById(R.id.txtUser);
		TextView txtName = (TextView) vContent.findViewById(R.id.txtName);
		TextView txtLiving = (TextView) vContent.findViewById(R.id.txtLiving);
		TextView txtAbout = (TextView) vContent.findViewById(R.id.txtAbout);
		TextView txtScore = (TextView) vContent.findViewById(R.id.txtScore);
		if (model!=null) {
			ImageLoader.getInstance().displayImage(JQuizEnglishApi.BASE_IMAGE_URL+model.avatar, imgAvatar,ProfileScreen.options);
			txtUser.setText("("+model.user+")");
			txtName.setText(model.name);
			txtLiving.setText(model.living_in);
			txtAbout.setText(model.about_you);
			txtScore.setText(""+model.total_score);
		}
		Button btnClose = (Button) vContent.findViewById(R.id.btnClose);
		btnClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		int dialogWidth = (MainActivity.SCREEN_WIDTH*8)/9;
		dialog.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
		dialog.show();
	}
	
	public static void showInputDialog(Activity act,String title,String message,final OnDialogListener listener){
		final Dialog dialog = new Dialog(act);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(act).inflate(R.layout.layout_edit, null);
		TextView tvTitle = (TextView) vContent.findViewById(R.id.title);
		final EditText edtInput = (EditText) vContent.findViewById(R.id.edtInput);
		edtInput.setText(message);
		tvTitle.setText(title);
		Button btnOK = (Button) vContent.findViewById(R.id.btnOK);
		Button btnCancel = (Button) vContent.findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onPositive(edtInput.getText().toString().trim());
				}
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (listener!=null) {
					listener.onCancel();
				}
			}
		});
		int dialogWidth = MainActivity.SCREEN_WIDTH*3/4;
		dialog.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
		dialog.show();
	}
	/**
	 * The listener interface for receiving onDialog events.
	 * The class that is interested in processing a onDialog
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addOnDialogListener<code> method. When
	 * the onDialog event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see OnDialogEvent
	 */
	public interface OnDialogListener{
		
		/**
		 * On positive.
		 *
		 * @param result the result
		 */
		public void onPositive(String result);
		
		/**
		 * On cancel.
		 */
		public void onCancel();
	}
}
